<!DOCTYPE html>
<html>

<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8">
    
    <meta http-equiv="content-language" content="zh-CN" />
    

    
    <meta name="viewport" content="width=device-width, initial-scale=0.5">
    

    
    <title>matlab 问题集合基础</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/clipboard.js/2.0.8/clipboard.min.js"></script>
    
    
    
    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css">

    
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap-theme.min.css">

    <link rel="stylesheet" href="/css/stylesheet.css">
    <link rel="stylesheet" href="/css/home.css">

    
    
        <style type="text/css">
        body { background-color: #fbf6ec;}
        </style>
    
    
                
        
        
            <link rel="stylesheet" href="/css/main.css"/>
        




        
        
        
        <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/styles/github.min.css"  />
         
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/highlight.min.js"></script>
        
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/r.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/yaml.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/latex.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/matlab.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/mathematica.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/julia-repl.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/powershell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/bash.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/shell.min.js"></script>
        
        <script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/10.3.2/languages/python.min.js"></script>
        
        <script>hljs.initHighlightingOnLoad();</script>
     <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
          
     <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css" integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA==" crossorigin="anonymous" />
     
     
</head>


<body>
    <script>
        window.addEventListener("resize", resizeThrottler, false);

        var resizeTimeout;
        function resizeThrottler() {
        
        if ( !resizeTimeout ) {
            resizeTimeout = setTimeout(function() {
            resizeTimeout = null;
            actualResizeHandler();
        
            
            }, 66);
        }
        }
        actualResizeHandler()
        function actualResizeHandler() {
                if (/mobile/i.test(navigator.userAgent) || /android/i.test(navigator.userAgent))
                {
                    document.body.classList.add('mobile');
                }else{
                    document.body.classList.remove('mobile');  
                }
    }</script>

    
      
      
            <nav class="navbar navbar-default navbar-static-top" style="opacity: .9" role="navigation">
        <div class="container-fluid">
            
            <div class="navbar-header">
                <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">

                    <span class="sr-only">Toggle navigation</span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>
                    <span class="big-icon icon-bar"></span>

                </button>
                <a class="navbar-brand" href="/">zsc</a>
            </div>

            <div class="navbar-collapse collapse" id="bs-example-navbar-collapse-1" style="height: auto;">
                <ul class="nav navbar-nav navbar-right" style="font-size: 100%">
                    
                        
                            
                            <li class=""><a href="/about/">About</a></li>
                            
                            <li class=""><a href="/categories/">Categories</a></li>
                            
                            <li class=""><a href="/">Home</a></li>
                            
                            <li class=""><a href="/tags/">Tags</a></li>
                            
                            <li class=""><a href="/issue/">存在的问题</a></li>
                            
                        
                    
                </ul>
            </div>
        </div>
    </nav>










<div class="inner">
    



    <div class="blog-post">
        
                <div>
            <h2 align="center" id = "singe-h2">
                matlab 问题集合基础
                <time>
                    <br>
                    <span> 
                        <i class="fa fa-user-edit" style="color:#888;font-size: 80%;"></i>
                        zsc 
                    </span>
                    &nbsp 
                    <span>                 
                        <i class="fa fa-calendar-alt" style="color:#888;font-size: 80%;"></i>
                        2019-11-13 
                    </span>
                </time>
                
                
                <div>
                    <ul class="tags">
                        
                        <span>标签:</span>
                        <li><a class="link" href="/tags/matlab"> #matlab </a></li>
                        
                        <span> </span>
                        
                    </ul>
                    
                </div>
            </h2>
        </div>
    
        
        <section id="content">
            <h2 id="1inputname获取函数参数名字返回一个字符串">1、<code>inputname</code>获取函数参数名字,返回一个字符串</h2>
<h2 id="2-输入可变长参数以及输出可变长参数">2、 输入可变长参数,以及输出可变长参数</h2>
<h2 id="3-nargin-捕获输入参数的个数-nargout-捕获输出参数的个数">3、 <code>nargin</code> 捕获输入参数的个数, <code>nargout</code> 捕获输出参数的个数.</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-MATLAB" data-lang="MATLAB"><span style="display:flex;"><span><span style="color:#66d9ef">function</span> varargout = <span style="color:#a6e22e">foo</span>(varargin)
</span></span><span style="display:flex;"><span><span style="color:#75715e">%输入可变长参数,以及输出可变长参数,都是放在一个细胞中存储, </span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%而inputname 存变量名的方式,用的是字符数组矩阵的形式</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">if</span> nargin <span style="color:#f92672">==</span> <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span>  varargout{<span style="color:#ae81ff">1</span>} = a1; varargout{<span style="color:#ae81ff">2</span>} = a2;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">else</span> nargin <span style="color:#f92672">==</span> <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span>  varargout{<span style="color:#ae81ff">1</span>} = a1; varargout{<span style="color:#ae81ff">2</span>} = a2; varargout{<span style="color:#ae81ff">3</span>} = a3;
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">function</span> varargout = <span style="color:#a6e22e">combin_index</span>(varargin)
</span></span><span style="display:flex;"><span>variable_name = cell(<span style="color:#ae81ff">1</span>,nargin); <span style="color:#75715e">% 创建一个 1* nargin 大小的 cell,</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">for</span> i = <span style="color:#ae81ff">1</span>:nargin
</span></span><span style="display:flex;"><span>    T = f(varargin{i});  <span style="color:#75715e">% 获取的新参数 传递给另一个函数</span>
</span></span><span style="display:flex;"><span>    variable_name{i} = inputname(i); <span style="color:#75715e">% 由于变量名是字符,所以用细胞来存储</span>
</span></span><span style="display:flex;"><span><span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% variable_name 这个 cell 存储了变量名</span>
</span></span></code></pre></div><h2 id="4-table-类型的变量如何进行转置对于表格您可以使用table2cell和cell2table的组合">4、 table 类型的变量如何进行转置,对于表格，您可以使用&rsquo;table2cell&rsquo;和&rsquo;cell2table&rsquo;的组合：</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-MATLAB" data-lang="MATLAB"><span style="display:flex;"><span>Xc = table2cell(X) <span style="color:#75715e">% 这里 X 为要转置的表,Xt 为转置后的表</span>
</span></span><span style="display:flex;"><span>Xt = cell2table(Xc<span style="color:#f92672">&#39;</span>,<span style="color:#e6db74">&#39;RowNames&#39;</span>,X.Properties.VariableNames,<span style="color:#e6db74">&#39;VariableNames&#39;</span>,X.Properties.RowNames)
</span></span></code></pre></div><p>如果用的是 matlab2018 可以使用<code>rows2vars</code>函数,参考https://www.mathworks.com/help/matlab/ref/rows2vars.html</p>
<h2 id="5-matlab字符串以及-cell-之间-的操作可以看薛山的-matlab-基础教程">5、 matlab字符串以及 cell 之间 的操作可以看薛山的 MATLAB 基础教程</h2>
<h2 id="6-区分repmat--以及repelem-">6、 区分<code>repmat</code>  以及<code>repelem</code> ,</h2>
<ul>
<li><code>repmat</code>  把复制的对象看做一个整体</li>
<li><code>repelem</code> 复制对象中的元素,最终结果不改变原对象的数据类型</li>
</ul>
<h2 id="7matlab-中-矩阵-点除--一个向量的问题">7、MATLAB 中 矩阵 点除  一个向量的问题(<code>./</code>),</h2>
<p>点除保证矩阵维度和向量长度相同,且 <code>w</code>为行向量,<code>A</code>为方阵,A 可以不是方阵,</p>
<p>MATLAB:</p>
<ul>
<li><strong><code> A ./ w</code>     w 为行向量时, 结果是把矩阵 A 的每一列对应的行向量元素相除</strong></li>
<li><strong><code>A ./ w' </code>    w&rsquo; 为列向量时, 结果是把矩阵 A 的每一行对应的列向量元素相除</strong></li>
</ul>
<p>R :  由于 R 中默认把向量都变为行向量, 所以想对列操作需要进行转变</p>
<ul>
<li><code> A / w</code>     w 为行向量时, 结果是把矩阵 A 的每一列对应的行向量元素相除</li>
<li><code>t(t(A)/w) </code>  其中w为行向量时, 结果是把矩阵 A 的每一行对应的行向量元素相除</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-MATLAB" data-lang="MATLAB"><span style="display:flex;"><span> <span style="color:#75715e">%% 实例</span>
</span></span><span style="display:flex;"><span> <span style="color:#f92672">&gt;&gt;</span> A1 = magic(<span style="color:#ae81ff">3</span>)
</span></span><span style="display:flex;"><span>A1 =
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">8</span>     <span style="color:#ae81ff">1</span>     <span style="color:#ae81ff">6</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">3</span>     <span style="color:#ae81ff">5</span>     <span style="color:#ae81ff">7</span>
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">4</span>     <span style="color:#ae81ff">9</span>     <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> d = [<span style="color:#ae81ff">1</span>:<span style="color:#ae81ff">3</span>]
</span></span><span style="display:flex;"><span>d =
</span></span><span style="display:flex;"><span>     <span style="color:#ae81ff">1</span>     <span style="color:#ae81ff">2</span>     <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> A1 <span style="color:#f92672">./</span> d
</span></span><span style="display:flex;"><span>ans =
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">8.0000</span>    <span style="color:#ae81ff">0.5000</span>    <span style="color:#ae81ff">2.0000</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">3.0000</span>    <span style="color:#ae81ff">2.5000</span>    <span style="color:#ae81ff">2.3333</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">4.0000</span>    <span style="color:#ae81ff">4.5000</span>    <span style="color:#ae81ff">0.6667</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> A1 <span style="color:#f92672">./</span> d<span style="color:#f92672">&#39;</span>
</span></span><span style="display:flex;"><span>ans =
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">8.0000</span>    <span style="color:#ae81ff">1.0000</span>    <span style="color:#ae81ff">6.0000</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.5000</span>    <span style="color:#ae81ff">2.5000</span>    <span style="color:#ae81ff">3.5000</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.3333</span>    <span style="color:#ae81ff">3.0000</span>    <span style="color:#ae81ff">0.6667</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%% 于是可以进行 按列单位化 :  A5 ./ sqrt(sum(A5.^2,1))</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">%%  按行单位化: A5 ./ sqrt(sum(A5.^2,2))</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#75715e"># R 中, 矩阵除以向量  直接是矩阵行除向量对应的元素. </span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> A <span style="color:#f92672">=</span> matlab<span style="color:#f92672">::</span><span style="color:#a6e22e">magic</span>(<span style="color:#ae81ff">3</span>)
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> A
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>] [,<span style="color:#ae81ff">2</span>] [,<span style="color:#ae81ff">3</span>]
</span></span><span style="display:flex;"><span>[1,]    <span style="color:#ae81ff">8</span>    <span style="color:#ae81ff">1</span>    <span style="color:#ae81ff">6</span>
</span></span><span style="display:flex;"><span>[2,]    <span style="color:#ae81ff">3</span>    <span style="color:#ae81ff">5</span>    <span style="color:#ae81ff">7</span>
</span></span><span style="display:flex;"><span>[3,]    <span style="color:#ae81ff">4</span>    <span style="color:#ae81ff">9</span>    <span style="color:#ae81ff">2</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> d <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#ae81ff">3</span>)
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> A <span style="color:#f92672">/</span> d
</span></span><span style="display:flex;"><span>         [,<span style="color:#ae81ff">1</span>] [,<span style="color:#ae81ff">2</span>]      [,<span style="color:#ae81ff">3</span>]
</span></span><span style="display:flex;"><span>[1,] <span style="color:#ae81ff">8.000000</span>  <span style="color:#ae81ff">1.0</span> <span style="color:#ae81ff">6.0000000</span>
</span></span><span style="display:flex;"><span>[2,] <span style="color:#ae81ff">1.500000</span>  <span style="color:#ae81ff">2.5</span> <span style="color:#ae81ff">3.5000000</span>
</span></span><span style="display:flex;"><span>[3,] <span style="color:#ae81ff">1.333333</span>  <span style="color:#ae81ff">3.0</span> <span style="color:#ae81ff">0.6666667</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">t</span>(<span style="color:#a6e22e">t</span>(A)<span style="color:#f92672">/</span>d)  <span style="color:#75715e"># 类似 MATLAB 矩阵列除以向量对应的元素</span>
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>] [,<span style="color:#ae81ff">2</span>]      [,<span style="color:#ae81ff">3</span>]
</span></span><span style="display:flex;"><span>[1,]    <span style="color:#ae81ff">8</span>  <span style="color:#ae81ff">0.5</span> <span style="color:#ae81ff">2.0000000</span>
</span></span><span style="display:flex;"><span>[2,]    <span style="color:#ae81ff">3</span>  <span style="color:#ae81ff">2.5</span> <span style="color:#ae81ff">2.3333333</span>
</span></span><span style="display:flex;"><span>[3,]    <span style="color:#ae81ff">4</span>  <span style="color:#ae81ff">4.5</span> <span style="color:#ae81ff">0.6666667</span>
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">## 还可以利用 sweep 函数,-- </span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">#矩阵的某个方向(列或行) 对一个向量中的元素进行一一对应运算</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">sweep</span>(A, <span style="color:#ae81ff">1</span>, d, FUN <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;/&#39;</span>)   <span style="color:#75715e"># 1 代表对矩阵行方向进行运算,等价于: A / d</span>
</span></span><span style="display:flex;"><span>         [,<span style="color:#ae81ff">1</span>] [,<span style="color:#ae81ff">2</span>]      [,<span style="color:#ae81ff">3</span>]
</span></span><span style="display:flex;"><span>[1,] <span style="color:#ae81ff">8.000000</span>  <span style="color:#ae81ff">1.0</span> <span style="color:#ae81ff">6.0000000</span>
</span></span><span style="display:flex;"><span>[2,] <span style="color:#ae81ff">1.500000</span>  <span style="color:#ae81ff">2.5</span> <span style="color:#ae81ff">3.5000000</span>
</span></span><span style="display:flex;"><span>[3,] <span style="color:#ae81ff">1.333333</span>  <span style="color:#ae81ff">3.0</span> <span style="color:#ae81ff">0.6666667</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">sweep</span>(A, <span style="color:#ae81ff">2</span>, d, FUN <span style="color:#f92672">=</span> <span style="color:#e6db74">&#39;/&#39;</span>)  <span style="color:#75715e"># 2 代表对矩阵列方向进行运算,等价于: t(t(A)/d)</span>
</span></span><span style="display:flex;"><span>     [,<span style="color:#ae81ff">1</span>] [,<span style="color:#ae81ff">2</span>]      [,<span style="color:#ae81ff">3</span>]
</span></span><span style="display:flex;"><span>[1,]    <span style="color:#ae81ff">8</span>  <span style="color:#ae81ff">0.5</span> <span style="color:#ae81ff">2.0000000</span>
</span></span><span style="display:flex;"><span>[2,]    <span style="color:#ae81ff">3</span>  <span style="color:#ae81ff">2.5</span> <span style="color:#ae81ff">2.3333333</span>
</span></span><span style="display:flex;"><span>[3,]    <span style="color:#ae81ff">4</span>  <span style="color:#ae81ff">4.5</span> <span style="color:#ae81ff">0.6666667</span>
</span></span></code></pre></div><blockquote>
<p><code>sweep(x, MARGIN, STATS, FUN=&quot;-&quot;, ...) </code>对矩阵进行运算。MARGIN为1，表示行的方向上进行运算，为2表示列的方向上运算。STATS是运算的参数。FUN为运算函数，默认是减法。下面利用sweep对矩阵x进行极差标准化变换。</p>
</blockquote>
<p>注意:</p>
<ul>
<li><strong>R 中 1 代表对行方向上进行运算, 2 代表对列的方向上进行运算,</strong></li>
<li><strong>MATLAB 中 1 代表对列方向上进行计算, 2 代表对行方向上进行计算</strong></li>
</ul>
<h2 id="8--矩阵对向量运算的理解"><strong>8  、矩阵对向量运算的理解</strong></h2>
<p>由于 R 中不存在列向量与行向量, 其实 R 中的向量在内存中都是以列的方式存储,所以 R 中的向量全是列向量, 这就可以和 MATLAB 的运算对应起来, 于是 MATLAB 和 R 的矩阵对向量运算可以解释为一下情况, 假设 <code>A</code> 代表矩阵,<code> d</code> 代表向量,<code>&amp;</code>代表运算规则</p>
<blockquote>
<p>A &amp; d</p>
</blockquote>
<p>1, 列向量<code>d</code>自动扩充复制成一个大小和矩阵<code>A</code> 一模一样的矩阵,然后再按照矩阵<code>点除(这里这的是矩阵对应位置元素做运算)</code>的方法进行运算**(即列向量按照行进行扩充)**</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span>d <span style="color:#f92672">=</span> <span style="color:#a6e22e">c</span>(<span style="color:#ae81ff">1</span><span style="color:#f92672">:</span><span style="color:#ae81ff">3</span>) <span style="color:#75715e">#列向量按照行进行扩充复制</span>
</span></span><span style="display:flex;"><span>[1 <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">1</span>
</span></span><span style="display:flex;"><span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">2</span> 
</span></span><span style="display:flex;"><span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">3</span> <span style="color:#ae81ff">3</span>]
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span>d <span style="color:#f92672">=</span> [1,<span style="color:#ae81ff">2</span>,<span style="color:#ae81ff">3</span>] <span style="color:#75715e"># 行向量按照列进行扩充</span>
</span></span><span style="display:flex;"><span>[1 <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>
</span></span><span style="display:flex;"><span> <span style="color:#ae81ff">1</span> <span style="color:#ae81ff">2</span> <span style="color:#ae81ff">3</span>]
</span></span></code></pre></div><p>2 由于 MATLAB 支持行向量,所以 行向量是按照列的方式进行复制扩充的.  然后在进行矩阵运算**(行向量按照列进行扩充)**</p>
<h2 id="9-两个向量之间的点除-">9 、两个向量之间的点除<code>./</code> ,</h2>
<ul>
<li>行向量点除列向量 ,  行作为分子,列作为分母,交叉构建成一个矩阵</li>
<li>列向量点除行向量,列作为分子, 行作为分母,交叉构建成一个矩阵</li>
<li>若$w = (w_1,w_2,\dots,w_n)$是一个向量,
<ul>
<li>数学中的$\dfrac{w_i}{w_j}$ , 对应的是 <code>w ./ w'</code>矩阵中的第<code>j,i</code> 元素</li>
<li>数学中的$\dfrac{w_j}{w_i}$ , 对应的是 <code>w ./ w'</code>矩阵中的第<code>i,j</code> 元素, 分母的索引在前面</li>
<li>matlab中<code>log(A).^2  </code> 等价<code>(log(A)).^2 </code></li>
</ul>
</li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> format rat
</span></span><span style="display:flex;"><span>w1 = [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">5</span>];
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> w2 = [<span style="color:#ae81ff">7</span>,<span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">11</span>];
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> w1 <span style="color:#f92672">./</span> w2<span style="color:#f92672">&#39;</span>
</span></span><span style="display:flex;"><span>ans =
</span></span><span style="display:flex;"><span>       <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>            <span style="color:#ae81ff">3</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>            <span style="color:#ae81ff">5</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>     
</span></span><span style="display:flex;"><span>       <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>            <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>            <span style="color:#ae81ff">5</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>     
</span></span><span style="display:flex;"><span>       <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">11</span>           <span style="color:#ae81ff">3</span><span style="color:#f92672">/</span><span style="color:#ae81ff">11</span>           <span style="color:#ae81ff">5</span><span style="color:#f92672">/</span><span style="color:#ae81ff">11</span>    
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> w1<span style="color:#f92672">&#39;</span> <span style="color:#f92672">./</span> w2
</span></span><span style="display:flex;"><span>ans =
</span></span><span style="display:flex;"><span>       <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>            <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>            <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">11</span>    
</span></span><span style="display:flex;"><span>       <span style="color:#ae81ff">3</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>            <span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>            <span style="color:#ae81ff">3</span><span style="color:#f92672">/</span><span style="color:#ae81ff">11</span>    
</span></span><span style="display:flex;"><span>       <span style="color:#ae81ff">5</span><span style="color:#f92672">/</span><span style="color:#ae81ff">7</span>            <span style="color:#ae81ff">5</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>            <span style="color:#ae81ff">5</span><span style="color:#f92672">/</span><span style="color:#ae81ff">11</span>  
</span></span></code></pre></div><h2 id="10-sum-prodmean--涉及维度的函数这和-r-中不一样默认都是对列进行操作"><strong>10、 <code>sum, prod,mean ,…</code> 涉及维度的函数,这和 R 中不一样,默认都是对列进行操作</strong></h2>
<ul>
<li><strong>R 中 一般是 1 代表对行进行操作,<code>apply(A,1,sum)</code></strong></li>
<li><strong>MATLAB 一般 1 代表是对列进行操作.  <code>sum(A,1)</code></strong></li>
</ul>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#75715e">% matlab</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> A2 = [<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">8</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>;
</span></span><span style="display:flex;"><span>      <span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">8</span>,<span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">1</span>];
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> sum(A2,<span style="color:#ae81ff">1</span>)
</span></span><span style="display:flex;"><span>ans =
</span></span><span style="display:flex;"><span>   <span style="color:#ae81ff">18.0000</span>    <span style="color:#ae81ff">9.4500</span>   <span style="color:#ae81ff">14.3333</span>    <span style="color:#ae81ff">1.3472</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> sum(A2,<span style="color:#ae81ff">2</span>)
</span></span><span style="display:flex;"><span>ans =
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">1.6444</span>
</span></span><span style="display:flex;"><span>   <span style="color:#ae81ff">10.1250</span>
</span></span><span style="display:flex;"><span>    <span style="color:#ae81ff">4.3611</span>
</span></span><span style="display:flex;"><span>   <span style="color:#ae81ff">27.0000</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;&gt;</span> sum(A2)
</span></span><span style="display:flex;"><span>ans =
</span></span><span style="display:flex;"><span>   <span style="color:#ae81ff">18.0000</span>    <span style="color:#ae81ff">9.4500</span>   <span style="color:#ae81ff">14.3333</span>    <span style="color:#ae81ff">1.3472</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-R" data-lang="R"><span style="display:flex;"><span><span style="color:#75715e">#  R语言</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> A <span style="color:#f92672">=</span> <span style="color:#a6e22e">matrix</span>(<span style="color:#a6e22e">c</span>(<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>,
</span></span><span style="display:flex;"><span><span style="color:#f92672">+</span>       <span style="color:#ae81ff">5</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">8</span>,
</span></span><span style="display:flex;"><span><span style="color:#f92672">+</span>       <span style="color:#ae81ff">3</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">4</span>,<span style="color:#ae81ff">1</span>,<span style="color:#ae81ff">1</span><span style="color:#f92672">/</span><span style="color:#ae81ff">9</span>,
</span></span><span style="display:flex;"><span><span style="color:#f92672">+</span>       <span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">8</span>,<span style="color:#ae81ff">9</span>,<span style="color:#ae81ff">1</span>),ncol <span style="color:#f92672">=</span><span style="color:#ae81ff">4</span>,nrow <span style="color:#f92672">=</span><span style="color:#ae81ff">4</span>,byrow <span style="color:#f92672">=</span> T)
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> 
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">apply</span>(A,<span style="color:#ae81ff">1</span>, sum)
</span></span><span style="display:flex;"><span>[1]  <span style="color:#ae81ff">1.644444</span> <span style="color:#ae81ff">10.125000</span>  <span style="color:#ae81ff">4.361111</span> <span style="color:#ae81ff">27.000000</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> <span style="color:#a6e22e">apply</span>(A,<span style="color:#ae81ff">2</span>, sum)
</span></span><span style="display:flex;"><span>[1] <span style="color:#ae81ff">18.000000</span>  <span style="color:#ae81ff">9.450000</span> <span style="color:#ae81ff">14.333333</span>  <span style="color:#ae81ff">1.347222</span>
</span></span><span style="display:flex;"><span><span style="color:#f92672">&gt;</span> 
</span></span></code></pre></div><h2 id="11-matlab归一化问题">11、 matlab归一化问题</h2>
<p>均值-方差归一化</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span><span style="color:#75715e">%自带函数 -- MATLAB 默认的方差是 n-1 的</span>
</span></span><span style="display:flex;"><span>zscore(A1)
</span></span><span style="display:flex;"><span>mapstd(A1)
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 矩阵方法 -- 其中 std(A1,0,1) : 0 方差是 n-1,</span>
</span></span><span style="display:flex;"><span>(A1 <span style="color:#f92672">-</span> mean(A1,<span style="color:#ae81ff">1</span>) ) <span style="color:#f92672">./</span> std(A1,<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">1</span>)
</span></span><span style="display:flex;"><span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 循环方法</span>
</span></span><span style="display:flex;"><span> n = size(A1,<span style="color:#ae81ff">1</span>)
</span></span><span style="display:flex;"><span> B = zeros(size(A1))
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">for</span> i = <span style="color:#ae81ff">1</span>:n
</span></span><span style="display:flex;"><span>     B(:,i) =( A1(:,i) <span style="color:#f92672">-</span> mean(A1(:,i)) ) <span style="color:#f92672">./</span> std(A1(:,i),<span style="color:#ae81ff">0</span>,<span style="color:#ae81ff">1</span>);
</span></span><span style="display:flex;"><span> <span style="color:#66d9ef">end</span>
</span></span><span style="display:flex;"><span> B
</span></span></code></pre></div><p>Max-min 均值化</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>mapminmax
</span></span></code></pre></div><h2 id="12-统计重复出现的此次即频率">12、 —统计重复出现的此次,即频率</h2>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>tabulate
</span></span></code></pre></div><h2 id="13---matlab-保存图片问题">13、   Matlab 保存图片问题</h2>
<p>保存图片 saveas 图片丢失颜色 —加参数 <code>epsc</code> 即可</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>saveas(gca,<span style="color:#e6db74">&#39;Density.eps&#39;</span>,<span style="color:#e6db74">&#39;epsc&#39;</span>)
</span></span></code></pre></div><p>saveas 指定目录保存</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>saveas(gcf,[<span style="color:#e6db74">&#39;D://give/your/path/here/&#39;</span>,<span style="color:#e6db74">&#39;&#39;</span>filename<span style="color:#f92672">&#39;</span>,bmp);
</span></span></code></pre></div><h2 id="14matlab-对缺失值-nan-的处理">14、matlab 对缺失值 nan 的处理</h2>
<p><code>R = rmmissing(A)</code> 从数组或表中删除缺失的条目</p>
<div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>rmmissing(B,<span style="color:#ae81ff">1</span>) <span style="color:#75715e">% nan 所在的行被删除,返回剩余完整的行 (默认)</span>
</span></span><span style="display:flex;"><span>rmmissing(B,<span style="color:#ae81ff">2</span>) <span style="color:#75715e">% nan 所在的列被删除,返回剩余完整的列</span>
</span></span></code></pre></div><div class="highlight"><pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;"><code class="language-matlab" data-lang="matlab"><span style="display:flex;"><span>B(all(<span style="color:#f92672">~</span>isnan(B), <span style="color:#ae81ff">2</span>),:)   <span style="color:#75715e">%同理删除矩阵中缺失的条目</span>
</span></span><span style="display:flex;"><span><span style="color:#75715e">% 其中 all(A,2) 的意思是如果A的某行没有非零值，那么该行返回值为1，</span>
</span></span></code></pre></div>
        </section>
    </div>
    <br>
    
    




<span id="/matlab/2019-11-13-matlab-question/" class="leancloud_visitors" data-flag-title="matlab 问题集合基础">
  <span class="post-meta-item-text">文章总阅读量 </span>
  <span class="leancloud-visitors-count"><i class="leancloud-visitors-count"></i></span>次;
  <p></p>
</span>



    

    
    
    <button id="edit-button" class="icon-button" type="button" title="Fork and edit" aria-label="Fork and edit" aria-haspopup="true" aria-expanded="false" aria-controls="edit">
        <i class="fa fa-edit">编辑本文</i>
    </button>
    
    
    

    <br>
    <hr>
    <li style="float:left;list-style:none">
        
        <a class="previous" href="/matlab/2019-11-13-matlab-fun-arg/"> 上一篇: MATLAB函数中参数的问题</a>
        
    </li>
    <li style="float:right;list-style:none">
        
        <a class="next" href="/matlab/2019-11-14-matlab_rcpp_c/"> 下一篇: Armadillo &#43; Rcpp  VS  MATLAB/octave</a>
        
    </li>
     
    
    <script src="/js/copyCode.js"></script>
    <script src="/js/tooltips.js"></script>
    
   
    <script>
    [].slice.call(document.querySelectorAll('table')).forEach(function(el) {
        var wrapper = document.createElement('div');
        wrapper.className = 'table-area';
        el.parentNode.insertBefore(wrapper, el);
        el.parentNode.removeChild(el);
        wrapper.appendChild(el);
        $("table").wrap("<div class='table-area'></div>");
    })
    </script>

    
<br>
<hr>


<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-111691389-1"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag() { dataLayer.push(arguments); }
  gtag('js', new Date());

  gtag('config', 'UA-111691389-1');
</script>




      
      
      

       
      
      
      <script>
              document.getElementById("edit-button").addEventListener("click", function () {
                  var editWindow = window.open("https:\/\/github.com\/zoushucai\/blogmmm/edit/master/content/matlab\/2019-11-13-matlab-question.md");
              });</script>
      
          




<script>
  function resizeIframe(obj) {
    obj.style.height = obj.contentWindow.document.body.scrollHeight + 'px';
  } 
</script>



    </style>
    <script type="text/javascript">
    function showdiv(){
        document.getElementById("divtocTableOfContents").style.display="block";
        document.getElementById("strHref").innerHTML="目录收起-";
        document.getElementById('divTableOfContents').style.width="22%";
        document.getElementById('divTableOfContents').style.height="55%";
        document.getElementById('divTableOfContents').style.top="25%";
        document.getElementById('divTableOfContents').style.bottom="5%";
        document.getElementById("strHref").href="javascript:hidediv()";
    }
    function hidediv(){
        document.getElementById("divtocTableOfContents").style.display="none";
        document.getElementById("strHref").innerHTML="目录展开+";
        document.getElementById("strHref").href="javascript:showdiv()";
        document.getElementById('divTableOfContents').style.width="10%";
        document.getElementById('divTableOfContents').style.height="5%";
    }
    </script>
</body>

</html>
</div> 







    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/mathtex-script-type.min.js" integrity="sha384-LJ2FmexL77rmGm6SIpxq7y+XA6bkLzGZEgCywzKOZG/ws4va9fUVu2neMjvc3zdv" crossorigin="anonymous"></script>

    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.css">
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/katex.min.js"></script>
    <script defer src="https://cdn.jsdelivr.net/npm/katex@0.12.0/dist/contrib/auto-render.min.js"></script>
    <script>
        document.addEventListener("DOMContentLoaded", function() {
            renderMathInElement(document.body, {
            delimiters: [
                            {left: "$$", right: "$$", display: true},
                            {left: "$", right: "$", display: false},
                            {left: "\\(", right: "\\)", display: false},
                            {left: "\\[", right: "\\]", display: true}
                        ]
            });
        });
    </script>













<br>
<div class="inner">
              
            
          
          
  
          
  
  <div id="vcomments"></div>
  
  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  
  <script src='//unpkg.com/valine/dist/Valine.min.js'></script>
  <script type="text/javascript">
    new Valine({
        el: '#vcomments' ,
        appId: 'HfHPKPkLa0cBEDPcdBAHuqMv-gzGzoHsz',
        appKey: 'r5RJAasN8e0mB9sq6y9pEcX0',
        lang:'zh-CN',
        notify:  false , 
        verify:  false  ,
        avatar:'identicon', 
        placeholder: '说点什么吧...',
        visitor:  true 
    });
  </script>

</div>

<br>
<br>
<footer>
    <p style="float:right;margin-right: 5%;margin-top: 0%;">
        &copy; 2022 <a href="https://github.com/zoushucai">zsc</a>
      </p>
</footer>
<br>
<br>


